﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace libry
{
    class User
    {
        private int userId;
        private String userEmail;//假设邮箱具有唯一性，注册的时候需要发优先验证
        private String userPwd;//用户密码
        private int limitNum;//限制图书数量
        private int limitDays;//以天为单位
        private bool state;


        public int UserId { get => userId; set => userId = value; }
        public string UserEmail { get => userEmail; set => userEmail = value; }
        public int LimitNum { get => limitNum; set => limitNum = value; }
        public int LimitDays { get => limitDays; set => limitDays = value; }
        public string UserPwd { get => userPwd; set => userPwd = value; }
        public bool State { get => state; set => state = value; }

        public void returnBook(String bookLocatedId)
        {
            BookBorrowServer.returnedBook(userId, bookLocatedId);
            return;
        }
        public void borrowBook(String bookLocatedId)
        {
            BookBorrowServer.borrowBook(userId, bookLocatedId);
            return;
        }
        //借阅信息显示
        public DataTable queryBorrowAll()
        {
            DataTable dt0 = sqlUser.getBookBorrow("BookBorrowWithName", "", userEmail, "借阅中");
            DataTable dt1 = sqlUser.getBookBorrow("BookBorrowWithName", "", userEmail, "已归还");
            DataTable dt = dt0.Clone();
            foreach (DataRow row in dt0.Rows)
            {
                dt.ImportRow(row);
            }
            foreach (DataRow row in dt1.Rows)
            {
                dt.ImportRow(row);
            }
            return dt;
        }
        //借阅信息查询
        public DataTable queryBorrow(String bookName, String returned)
        {
            return sqlUser.getBookBorrow("BookBorrowWithName", bookName, userEmail, returned);
        }

        //提早归还
        public void urgeReturn(String bookLocated)
        {

        }

        //修改账号密码，之前要先进行二维码验证以及判断原邮箱和密码和新的是否一样
        public void changeUser(String newEmail, String newPwd)
        {
            if (newEmail != "")
            {
                int affected = sqlUser.ChangeUser(userId, "userEmail = '" + newEmail + "'");
                if (affected > 0)
                {
                    MessageBox.Show("修改邮箱账号成功");
                }
                else
                {
                    MessageBox.Show("修改邮箱账号失败");
                }
            }
            if (newPwd != "")
            {
                int affected = sqlUser.ChangeUser(userId, "userPwd = '" + newPwd + "'");
                if (affected > 0)
                {
                    MessageBox.Show("修改账号密码成功");
                }
                else
                {
                    MessageBox.Show("修改账号密码失败");
                }
            }
        }

        //续借书，无限续借
        public void renewBook(String bookLocatedId, int days)
        {
            //判断该书是否已借出
            DataTable dt = sqlUser.getBookBorrowAll();
            DataRow[] dr = dt.Select("bookLocatedId = '" + bookLocatedId + "' and userId = '" + userId + "' and returned = '借阅中'");
            if (dr.Length > 0)
            {
                int id = Convert.ToInt32(dr[0]["id"]);
                DateTime time = DateTime.Now;
                String endTime = time.AddDays(days).ToString();
                //更改book_borrow
                int affected = sqlUser.ChangeBookBorrow(id, "endTime = '" + endTime + "'");
                if (affected > 0)
                {
                    MessageBox.Show("续借成功");
                }
                else
                {
                    MessageBox.Show("续借失败");
                }
            }
            else
            {
                MessageBox.Show("书和用户匹配不上或者该书已经归还");
            }
        }


    }

}
